<?php 
session_start();
require_once ("class/m_customer_history.php");

	$m = new M_customer_history();
	
	if (isset($_POST['m'])  &&  $_POST['m']!=""){
		if ($_POST['m']== 'info'){
			if(isset($_POST['id']) && $_POST['id']!=""){ 
				$id=$_POST['id'];
				//$id=1;
				echo json_encode($m->getDataInfo($id));
			}else{
				echo "Errro";
			}
		}else if ($_POST['m']== 'history'){
		
			$page=$_POST['page'];
			$page=isset($page)?$page:1;
			
			$result=$m->getData($_POST['id'],$page);
			//echo $result;
			$rows=$m->getRowCount();
			
			$k=1 + (($page-1) * $m->getListPage());

			if ($rows>0){
				while($row = $result->FetchRow()){
					If($k%2 <> 0){
						$html .= "<tr class=row1>";
					} else{
						$html .= "<tr>";
					}
					
					$html.="<td align='right'>".$k."</td>"
						."<td style='display:none'>".$row['ID']."</td>"
						."<td align='center'>".$row['Historydate']."</td>"
						."<td align='left' style='display:none'>".$row['BranchName']."</td>"
						."<td align='left'>".$row['ProductName']."</td>"
						."<td align='right'>".$row['Amount'].' - '.$row['Unit']."</td>"
						."<td align='right'>".$row['Price']."</td>"
						."<td align='right'>".$row['Discount']."</td>"
						."<td align='right'>".$row['Total']."</td>"
						."</tr>";
					$k++;
				}
			}else{
					$html="<tr>"
					."<td colspan='8' align='center' ><img src='./images/info.gif'/><b/></td>"
					."</tr>";
			}
			//Return result , rows , page
			$ar_result=array('data'=>$html,
							'rows'=>$rows,
							'totalpage'=>$m->getTotalPage()
							);
			echo json_encode( $ar_result );//$html;
					
		}else if ($_POST['m']== 'add'){
			$ar= $_POST['var'];
			$ar["CreatedBy"]="admin";
			$ar["CreatedDate"]=date( "Y/m/d H:i:s" );
			
			$id=$m->InsertData($ar);
 
			$ar["ID"]=$id;
			
			echo "<tr>".genTable($ar)."</tr>";
			
		}else if ($_POST['m']=='edit'){
			if (isset($_POST['var'])){
				$ar=$_POST['var'];
				$flag=isset($ar);

				$ar["UpdatedBy"]="admin";
				$ar["UpdatedDate"]=date( "Y/m/d H:i:s" );
				
				$sqlScript="select * from tb_position where IsDeleted=0 and ID='". $_POST['id'] ."'";
				$m->updateData($sqlScript, $ar);
 
				$ar["ID"]=$_POST['id'];
				echo genTable($ar);
			}
					
		}else if ($_POST['m']=='del'){
			if(isset($_POST['var'])){
				$o=$_POST['var'];
				$m->delData($o["id"]);
			}
		}else if ($_POST['m']== 'search'){
			$name=$_POST['name'];
			$page=$_POST['page'];
			$page=isset($page)?$page:1;	
			
			$result=$m->getData($name,$page);
			
			$rows=$m->getRowCount();
			$i=1;
			$html="";
			if ($rows>0){
				while($row = $result->FetchRow()){
					If($i%2 <> 0){
						$html .= "<tr class=row1>";
					} else{
						$html .= "<tr>";
					}
					$html .= genTable($row);
					$html .= "</tr>";
					$i++;
				}
			}else{
				$html="<tr>"
					."<td colspan=4 align='center' ><img src='./images/info.gif'/><b/></td>"
					."</tr>";
			}
			//Return result , rows , page
			$ar_result=array('data'=>$html,
							'rows'=>$rows,
							'totalpage'=>$m->getTotalPage());
			echo json_encode( $ar_result );
		}
	}else{echo"Error2";}

	function genTable($row){
			$s.="<td align='right'>".$row["ID"]."</td>" .
					"<td align='left'>".$row["PositionName"]."</td>" .
					"<td align='center'>".
					"<img src='./images/edit.png' class='btnEdit' style='cursor:pointer' title='Edit' border='0'/></td>".
					"<td><img src='./images/remove.png' class='btnDel' style='cursor:pointer' title='Delete' border='0'/></td>" ;
		return $s;
	}

?>